intro-to-functions
JavaScript Functions#
A JavaScript function is a block of code designed to perform a particular task. The main advantage of using functions is code reuse: Define the code once, and use it many times.
Use the same code many times with different arguments, to produce different results.
Function Declaration#
To define a JavaScript function, we use the function keyword, followed by a name followed by a set of parentheses ().
The code to be executed by the function is placed inside curly brackets {}.
Note: Function names can contain letters, digits, underscores, and dollar signs (same rules as variables) written in camel case. In addition, it's a best practice to actually tell what the function is doing by giving the function name a verb as prefix. This verb as prefix can be anything (e.g. get, fetch, push, apply, calculate, compute, post). It's a soft rule to consider for having more self-descriptive JavaScript functions.
Let's write our first function.
Function Invocation (calling a function)#
To execute the code inside the function you need to invoke it (call it). To call a function, start with the name of the function, then follow it with the parentheses.
Once the function is defined, JavaScript allows us to call it as many times as we want to.
Function Parameters#
Function can take parameters. Function parameters are the names listed inside the parentheses () in the function's definition.
Note: As with variables, parameters should be given names, which are separated by commas within the parentheses.
After defining the parameters, you can use them inside the function.
Function Arguments#
Function arguments are the real values passed to (and received by) the function. When calling a function, we provide the parameters value (arguments) inside the parentheses. We can define a single function, and pass different parameter values (arguments) to it.
Multiple Parameters#
We can define multiple parameters for a function by comma-separating them.
When calling a function, we must provide the arguments in the same order in which we defined them.
Note: JavaScript functions do not check the number of arguments received. If a function is called with missing arguments (fewer than declared), the missing values are set to undefined.
Function Return#
A function can have an optional return statement. It is used to return a value from the function. This statement is useful when making calculations that require a result. For example, let's calculate the product of two numbers, and return the result:
If we do not return anything from a function, it will return undefined.
When JavaScript reaches a return statement, the function stops executing.
Function Expression#
A JavaScript function can also be defined using an expression. A function expression can be stored in a variable.
After a function expression has been stored in a variable, the variable can be used as a function. Functions stored in variables do not need function names. They are always called using the variable name.
Note: With function expression, the function name can be omitted.